package com.aifast.system.fd.controller;

import com.aifast.common.annotations.AiIdempotent;
import com.aifast.common.base.Controller;
import com.aifast.common.base.R;
import com.aifast.common.layui.LayuiDataTable;
import com.aifast.system.fd.entity.FdArea;
import com.aifast.system.fd.service.FdAreaService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;

/**
 * 全国行政区划 控制层
 *
 * @author fujian
 * @date 2022-08-03
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/area")
public class FdAreaController extends Controller
{

    /**
    * 视图层跳转统一调度 （统一放在viewController）
    * @param path 页面名称
    */
    @GetMapping("/sys/area/{path}")
    public String area(@PathVariable String path){
        return "sys/area/" + path;
    }

    private final FdAreaService service;

    @GetMapping("list")
    public LayuiDataTable list() {
        return service.list(getSearchValue(), getPage());
    }

    @AiIdempotent
    @PostMapping("add")
    public R add(@Valid FdArea fdArea) {
        return service.insertOrUpdate(fdArea) ? OK() : ERROR();
    }

    @AiIdempotent
    @GetMapping("del")
    public R del() {
        return service.del(getInt("id"), get("ids")) ? OK() : ERROR();
    }

    @GetMapping("info")
    public R info() {
        return service.info(getInt("id"));
    }

    @GetMapping("export")
    public void export() throws IOException {
        service.export(getSearchValue(),response);
    }

}
